<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Form\Form;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Stdlib\Parameters;
use Zend\View\Model\ViewModel;
use Zend\Authentication\AuthenticationService;
use SanAuthWithDbSaveHandler\Storage\IdentityManagerInterface;
use Zend\View\Model\JsonModel;
use ZfcUser\Model\TuserTable;
use ZfcUser\Model\Tuser;
use ZfcUser\Model\Torders;
use ZfcUser\Model\TordersTable;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel();
    }
	public function learnAction()
    {
        return new ViewModel();
    }
	public function buyAction()
    {
		//unset($_SESSION);
        return new ViewModel();
    }
	public function confirmAction()
    {
		return new ViewModel();
    }
	public function payAction()
    {
        return new ViewModel();
    }
	public function addCartAction(){
		if(isset($_POST['packageName'])){
			$cart=array();
			$_SESSION['cart']['package_name']	= 	$_POST['packageName'];
			$_SESSION['cart']['price']			=	$_POST['value'];
			$result = new JsonModel(array(					
				'output' => 'success',
			));
			return $result;
		}
	}
	public function orderAction()
    {
		// Get User Id
		$uesrId='';
		if(isset($_SESSION['user'])){
			$uesrId=$_SESSION['user']['userId'];
		}else{
			$token = $this->getUniqueCode('10');
			$table = $this->getServiceLocator()->get('ZfcUser\Model\TuserTable');
			$user = $table->addUser($_POST,$token);
			$uesrId=$user->user_id;
		}
		//End
		
		// Insert Order
		$table = $this->getServiceLocator()->get('ZfcUser\Model\TordersTable');
		$order = $table->addOrder($_POST,$uesrId);
		$orderId=$order->order_id; 
		// End
		
		$data=array();
		if(isset($_POST['name'])){
			$data['user_name']=$_POST['name'];
			$data['user_address']=$_POST['address'];
			$data['user_zipcode']="522235";
			$data['user_city']=$_POST['city'];
			$data['user_state']=$_POST['state'];
			$data['user_country']="Ind";
			$data['user_phone']=$_POST['mobileNo'];
			$data['user_email']=$_POST['emailId'];
			$data['user_id']='"'.$uesrId.'"';
			$data['modelno']="M1";
			$data['key']="ebskey";
			$data['account_id']="5880";
			$data['finalamount']=20000;
			$data['order_no']="123456";
			$data['return_url']="http://extramarks.local/response?DR={DR}";
			$data['mode']="TEST";
			$data['hash'] = $data['key']."|".$data['account_id']."|".$data['finalamount']."|".$data['order_no']."|".$data['return_url']."|".$data['mode'];
			$data['secure_hash'] = md5($data['hash']);
		}
        $_SESSION['data']=$data;
		$result = new JsonModel(array(					
			'output' => 'success',
		));
		return $result;
    }
	public function paymentAction()
    {
        $result = new ViewModel(array(					
			'data' => $_SESSION['data'],
		));
		return $result;
    }
	public function responseAction()
    {
		$secret_key = "ebskey"; // Your Secret Key
		if(isset($_GET['DR'])) {
			require('/public/Rc43.php');
			$DR = preg_replace("/\s/","+",$_GET['DR']);
			//$rc4 = new Crypt_RC4($secret_key);
			$QueryString = base64_decode($DR);
			//$rc4->decrypt($QueryString);
			$QueryString = explode('&',$QueryString);
			$response = array();
			echo '<pre>'; print_r($QueryString); exit;
			foreach($QueryString as $param){
				$param = explode('=',$param);
				//$response[$param[0]] = urldecode($param[1]);
			}
		} 

		//payment sucess
		if(($response['ResponseCode'] == 0))
		{
			foreach( $response as $key => $value)
			{
				echo $key;
				echo $value;
			}
		}
		// payment failed
		if(($response['ResponseCode'] != 0))
		{
			foreach( $response as $key => $value)
			{
				echo $key;
				echo $value;
			}
		}exit; 
        return new ViewModel();
    }
	
	public function getUniqueCode($length = "")
	{
		$code = md5(uniqid(rand(), true));
		if ($length != "")
		return substr($code, 0, $length);
		else
		return $code;
	}
	
}
